import pymysql
from common.log import log
from conf.env import DB_ENV
def connect():
    try:
        a = pymysql.connect(user=DB_ENV["username"],
                            password=DB_ENV["password"],
                            host=DB_ENV["host"],
                            database=DB_ENV["dbname"],
                            port=DB_ENV["port"],
                            charset='utf8')
        log.info(f'连接数据库{DB_ENV["dbname"]}成功')
        return a
    except Exception as e:
        log.info(f"连接数据库异常，异常信息为{e}")
        raise e
def disconnect(conn):
    conn.close()
    print(f"数据库关闭正常")
def execute(conn, sql):
    try:
        c = conn.cursor()
        r = c.execute(sql)
        conn.commit()
        # test9527_1
        print(f"执行{sql}成功")
        return r
    except Exception as e:
        print(f"执行{sql}异常，异常信息为{e}")
def delete_member(phone):
    a = connect()
    # 常见错误 port 整数  pwd 字符串   charset utf8 不是 utf-8
    execute(a, f"delete from member where mobilephone={phone};")
    # execute(a, f"delete from member where mobilephone={phone}")   最后的分号 似乎没有影响
    disconnect(a)
if __name__ == '__main__':
    a = connect()
    # 常见错误 port 整数  pwd 字符串   charset utf8 不是 utf-8
    print(execute(a, "select * from sys_user "))
    # print(execute(a, "insert into xqdsys_user (account,password)values('test9529',MD5('123456')) "))
    # print(execute(a, "select * from xqdsys_user "))
    disconnect(a)